package com.zhang.family_doctor.service.impl;

import com.github.pagehelper.PageHelper;
import com.zhang.family_doctor.dto.RolePurviewDto;
import com.zhang.family_doctor.entity.DoctorRole;
import com.zhang.family_doctor.entity.R;
import com.zhang.family_doctor.entity.Role;
import com.zhang.family_doctor.entity.RolePurview;
import com.zhang.family_doctor.mapper.RoleMapper;
import com.zhang.family_doctor.service.RoleService;
import com.zhang.family_doctor.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public R save(Role role, HttpServletRequest request) {

        String tokenBackstage = request.getHeader("tokenBackstage");
        String getName = TokenUtils.verifyAndGetName(tokenBackstage);

        Integer id = UUID.randomUUID().toString().replaceAll("-","-").hashCode();
        id = id < 0 ? -id : id;

        role.setId(id);
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        role.setFounder(getName);

        roleMapper.save(role);
        return R.success("新增成功");
    }

    @Override
    public R update(Role role, HttpServletRequest request) {

        String tokenBackstage = request.getHeader("tokenBackstage");
        String getName = TokenUtils.verifyAndGetName(tokenBackstage);

        role.setUpdateTime(new Date());
        role.setFounder(getName);

        roleMapper.update(role);
        return R.success("角色修改成功！");
    }

    @Override
    public R<List<Role>> getAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Role> roleList = roleMapper.getAll();
        return R.success(roleList);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @Override
    public R<String> deleteById(Integer id) {

        List<DoctorRole> doctorRoleByRoleId = roleMapper.getDoctorRoleByRoleId(id);

        if (doctorRoleByRoleId.size() == 0){
            roleMapper.deleteById(id);
            return R.success("删除角色成功！");
        }else {
            return R.buildFailure("该角色下有医生！无法删除！");
        }
    }

    /**
     * 给角色新增权限
     * @param rolePurviewDto
     * @return
     */
    @Override
    public R<String> saveRolePurview(RolePurviewDto rolePurviewDto) {

        Integer roleId = rolePurviewDto.getRoleId();
        List<RolePurview> rolePurviewList = rolePurviewDto.getRolePurviewList();

        for (RolePurview rolePurview : rolePurviewList){

            Integer id = UUID.randomUUID().toString().replaceAll("-", "").hashCode();
            id = id < 0 ? -id : id;

            rolePurview.setId(id);
            rolePurview.setRoleId(roleId);
            roleMapper.saveRolePurview(rolePurview);
        }
        return R.success("新增成功！");
    }

    @Override
    public R<String> updateRolePurview(RolePurviewDto rolePurviewDto) {

        Integer roleId = rolePurviewDto.getRoleId();
        roleMapper.deleteRolePurviewByRoleId(roleId);

        List<RolePurview> rolePurviewList = rolePurviewDto.getRolePurviewList();
        for (RolePurview rolePurview : rolePurviewList){

            int id = UUID.randomUUID().toString().replaceAll("-", "-").hashCode();
            id = id < 0 ? -id : id ;

            rolePurview.setId(id);
            rolePurview.setRoleId(roleId);
            roleMapper.saveRolePurview(rolePurview);
        }

        return R.success("修改成功！");
    }


}
